package com.qding.park.manager.app.queue;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.core.redis.JedisClient;
import com.qding.park.manager.app.queue.util.SpringContextUtil;

/**
 * @ClassName AppContext
 * @Description TODO(App 处理上下文)
 * @author Administrator
 * @Date 2016年9月16日 下午12:32:15
 * @version 1.0.0
 */
@Component("appContext")
public class AppContext {

	private static final Log _log = LogFactory.getLog(AppContext.class);

	/**
	 * @Field @threadNum : TODO(queueAll 队列并发线程数)
	 */
	private int threadNum_queueAll = 1;

	/**
	 * @Fields threadNum_queueOrder : TODO（queueOrder 队列并发线程数）
	 */
	private int threadNum_queueOrder = 1;

	/**
	 * @Fields threadNum_queueReport : TODO（queue-report 处理线程数）
	 */
	private int threadNum_queueReport = 1;

	/**
	 * @Field @threadList : TODO(启动的线程列表)
	 */
	private List<Thread> threadList = new ArrayList<Thread>();

	// private static ThreadPoolTaskExecutor threadPool;

	@Autowired
	private JedisClient jedisClient;

	@Autowired
	private IExceptionHandle exceptionHandle;

	// @Autowired
	// private UserInfoSyncJobFacade userInfoSyncFacade;

	/**
	 * @Description (启动)
	 */
	public void startApp() {

		// try {
		// userInfoSyncFacade.userInfoSync();
		// } catch (AdapterProxyCreateFail | AdapterException | NetWorkException
		// e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// int num = (threadNum_queueAll > 0 ? threadNum_queueAll : 1);

		for (int i = 0; i < threadNum_queueAll; i++) {

			QueueAllRunTask task = new QueueAllRunTask(jedisClient, exceptionHandle);

			ThreadPoolTaskExecutor threadPool = SpringContextUtil.getApplicationContext()
					.getBean(ThreadPoolTaskExecutor.class);
			threadPool.execute(task);
		}

		// int num_01 = (threadNum_queueOrder > 0 ? threadNum_queueOrder : 1);

		for (int i = 0; i < threadNum_queueOrder; i++) {

			QueueOrderRunTask task = new QueueOrderRunTask(jedisClient, exceptionHandle);

			ThreadPoolTaskExecutor threadPool = SpringContextUtil.getApplicationContext()
					.getBean(ThreadPoolTaskExecutor.class);
			threadPool.execute(task);
		}

		for (int i = 0; i < threadNum_queueReport; i++) {

			QueueReportRunTask task = new QueueReportRunTask(jedisClient, exceptionHandle);

			ThreadPoolTaskExecutor threadPool = SpringContextUtil.getApplicationContext()
					.getBean(ThreadPoolTaskExecutor.class);
			threadPool.execute(task);
		}
	}
}
